package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.api.FileService;
import com.powernode.common.JwtUtil;
import com.powernode.common.LxlException;
import com.powernode.common.ResultJson;
import com.powernode.entity.UmsUser;
import com.powernode.mapper.UmsUserMapper;
import com.powernode.service.UmsUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lxl
 * @since 2022-03-27
 */
@Service
public class UmsUserServiceImpl extends ServiceImpl<UmsUserMapper, UmsUser> implements UmsUserService {

    @Resource
    private BCryptPasswordEncoder passwordEncoder;

    @Resource
    private FileService fileService;

    public IPage<UmsUser> list(Integer pageNo, Integer pageSize, String value) {
        //查询
        QueryWrapper<UmsUser> wrapper = new QueryWrapper<UmsUser>();
        wrapper.orderByDesc("id");
        if (StringUtils.isNotBlank(value)) {
            wrapper.like("name", value)
                    .or().like("phone", value)
                    .or().like("email", value);
        }
        return this.page(new Page<UmsUser>(pageNo, pageSize), wrapper);
    }

    public Boolean add(String name, String phone, String email, String password, MultipartFile file) {
        String path = fileService.upload(file,"image");
        UmsUser umsUser = new UmsUser(name, phone, email,path, null, passwordEncoder.encode(password));
        return this.save(umsUser);
    }

    public Boolean check(String fieldName, String value, Long id) {
        QueryWrapper<UmsUser> wrapper = new QueryWrapper<UmsUser>();
        wrapper.eq(fieldName, value);
        if (id != null) {
            //如果进来就证明是修改，必须跟不是我的id的对象的属性不重复
            wrapper.ne("id", id);
        }
        return this.count(wrapper) == 0;
    }

    public Boolean update(Long id, String name, String phone, String email, MultipartFile file) {
        String path = null;

        if (file != null && file.getSize() != 0) {
            path = fileService.upload(file,"image");
            System.out.println(path);
        }
        UmsUser umsUser = new UmsUser(name, phone, email, path, null, null);
        umsUser.setId(id);
        return this.updateById(umsUser);
    }

    public Boolean del(Long id, Integer active) {
        UmsUser umsUser = new UmsUser();
        umsUser.setId(id);
        umsUser.setActive(active);
        return this.updateById(umsUser);
    }

    public List<UmsUser> getActive() {
        QueryWrapper<UmsUser> wrapper = new QueryWrapper<UmsUser>();
        wrapper.eq("active",1);
        return this.list(wrapper);
    }

    @Override
    public String login(String username, String password) throws LxlException {
        QueryWrapper<UmsUser> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",username)
                .or().eq("email",username);
        UmsUser user = this.getOne(wrapper);
        if (user == null || !passwordEncoder.matches(password,user.getPassword())){
            //用户不存在
            throw new LxlException("用户名或密码错误");
        }
        if (user.getActive() == 0){
            //用户已经失效
            throw new LxlException("用户已经失效无法登录");
        }
        return JwtUtil.create(user);
    }
}
